const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show_haibao: false, //是否显示 海报生成框

    movies: [
      { url: "../image/list_15_03.png" },
      { url: "../image/list_15_03.png" },
      { url: "../image/list_15_03.png" },
      { url: "../image/list_15_03.png" }
    ],
    mySale:'none',
    panduan1:true,//详情
    panduan2: false,
    ztcolo: "colorbor",
    allcolo: "cur",
    comments: [],
    num: 1,
    minusStatus: 'disable',
    _num: 1,
    display_evaluate: "none",
    display_collageWare: "block",
    display_details: "none",
    _number: 0,
    num_ber: 1,
    display_confirm: "none",
    collect: "inline-block",
    collect_select: "none",
    detail: '',
    iscode: '',
    Type: '',
    goodsid: '',
    buyNum:'',
  },
  close_haibao(){
    this.setData({
      show_haibao:false
    })
  },
  // 打开分享操作表
  open_share_sheets(){
    let that = this;
    console.log(that.data.detail.goodsid)
  
    wx.showLoading({
      title: '生成海报中',
    })
    let create_post = function () {
      return new Promise(function (resolve, reject) {
        wx.request({
          url: app.data.Url + '/goods/GetPoster',
          data: {
            goods_id: that.data.detail.goodsid,
            path: '/pages/normal/normal',
            // id: that.data.goods_sid
          },
          method: 'post',
          success: function (res) {
            resolve(res)
          },
          complete: function () {
            wx.hideLoading();
          }
        })
      })
    }


    create_post().then(res => {
      that.setData({
        show_haibao: true,
        haibao_url: res.data.data.replace("http","https")
      })
      console.log(res.data)
    })

    
     
     
  },
  
 
  // 海报保存至相册
  save_to_xc(){
    let that = this;
    console.log("save_to_xc");
    wx.showLoading({
      title: '请稍后',
      mask: true,
  
    })
    wx.getImageInfo({
      src: this.data.haibao_url,
      success(res) {
        console.log(res)
        wx.saveImageToPhotosAlbum({
          filePath:res.path,
          success:function(){
            that.setData({
              show_haibao:false
            })
           
          },
          complete:function(){
            wx.hideLoading({

            })
          }
        })
      }
    })
    return
    
    this.setData({
      show_haibao: false
    })
  },
  ztshow: function () {
    this.setData({
      panduan1: true,
      panduan2: false,
      ztcolo: "colorbor",
      allcolo: "cur",
    })

  },
    allshow: function () {
    this.setData({
      panduan1: false,
      panduan2: true,
      ztcolo: "cur",
      allcolo: "colorbor",
    })
  },
  //优惠券打开
  sale: function () {
    this.setData({
      mySale: "block",
      // add_confirm: "block",
    })
  },
  hideview: function () {
    this.setData({
      display_confirm: "none",
      mySale: "none",
    })
  },
  // 增加数量
  addCount: function (e) {
    var num = this.data.num;
    num++;
    
    this.setData({
      num: num,
    })
  },
  // 减少数量
  minusCount: function (e) {
    var num = this.data.num;
    if (num > 1) {
      num--;
    }
    var minusStatus = num > 1 ? 'normal' : 'disable';
    this.setData({
      num: num,
      minusStatus: minusStatus
    })
  },
  focus_click: function (e) {
    var that = this
    this.setData({
      _num: e.target.dataset.num
    });
    if (e.target.dataset.num == 1) {
      this.setData({
        display_evaluate: "block",
        display_collageWare: "none",
        display_details: "none",
        comments: that.data.comments

      })
    };
    if (e.target.dataset.num == 2) {
      this.setData({
        display_evaluate: "none",
        display_collageWare: "block",
        display_details: "none"
      })
    };
  },
  has_focus_click: function (e) {
    let that = this;
    //var list = that.data.
    this.setData({
      names: e.currentTarget.dataset.index,
      _number: e.currentTarget.dataset.id
    })
  },
  //减少数量
  minus: function () {
    var num_ber = this.data.num_ber;
    if (num_ber > 1) {
      num_ber--;
    }
    var minusStatus = num_ber > 1 ? 'normal' : 'disable';
    this.setData({
      num_ber: num_ber,
      minusStatus: minusStatus
    })
  },
  //增加数量
  add: function (e) {
    var num_ber = this.data.num_ber;
    num_ber++;
    var minusStatus = num_ber > 1 ? 'normal' : 'disable';
    this.setData({
      num_ber: num_ber,
      minusStatus: minusStatus
    })
  },

  buy_now: function () {
    this.setData({
      display_confirm: "block",
      Type: 0
    })
  },
  buy_nows: function () {
    this.setData({
      display_confirm: "block",
      Type: 1
    })
  },
  buy_car:function(){
    wx.switchTab({
      url: '/pages/car/car'
    })
  },
  buy_home: function () {
    wx.switchTab({
      url: '/pages/index/index'
    })
  },
  // hideview: function () {
  //   this.setData({
  //     display_confirm: "none"
  //   })
  // },
  evaluate: function () {
    this.setData({
      display_evaluate: "block",
      display_collageWare: "none",
      display_details: "none"
    })
  },
  collect: function () {
    // this.setData({
    //   collect: "none",
    //   collect_select: "inline-block",
    //   collection: true
    // })
    let that = this
    var iscode = that.data.iscode;
    wx.request({
      url: app.data.Url + 'collect/addcollect',
      data: {
        uid: wx.getStorageSync('userInfo').uid,
        goodsid: that.data.detail.goodsid,
        status: iscode,
        type: 0
      },
      method: 'get',
      success: function (res) {
        if (res.data.msg == '收藏成功') {
          that.setData({
            iscode: 1
          })
          wx.showToast({
            title: '收藏成功',
          })
        }
        if (res.data.msg == '取消收藏') {
          that.setData({
            iscode: 0
          })
          wx.showToast({
            title: '取消收藏',
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options)
    let that = this
    // wx.showToast({
    //   title: options.id,
    //   duration: 20000
    // })
    /***商品详情 */
    wx.setStorage({
      key: "tid",
      data: options.tid
    })
    wx.request({
      url: app.data.Url + 'goods/detail',
      data: {
        goodsid: options.id,
        // goodsid:427237
      },
      method: 'get',
      success: function (res) {
        console.log('------------------',res)
        
        var WxParse = require('../../wxParse/wxParse.js');
        var result = res.data.data.content;
        WxParse.wxParse("result", "html", result, that, 5)
    
        if (res.data.data.specification == undefined || res.data.data.specification == '' || res.data.data.specification == null) {
          that.setData({
            detail: res.data.data,
          })
        } else {
          that.setData({
            detail: res.data.data,
            cate: res.data.data.specification[0],
            names: res.data.data.specification[0]
          })
        }

      }
    })
    var goodsid = options.id;
    wx.request({
      url: app.data.Url + 'collect/iscollect',
      data: {
        uid: wx.getStorageSync('userInfo').uid,
        goodsid: goodsid,
      },
      method: 'get',
      success: function (res) {
        if (res.data.code == 0) {
          that.setData({
            iscode: 0
          })
        } else {
          that.setData({
            iscode: 1
          })
        }

      }
    })
    /**
     * 商品评价
     */
    wx.request({
      url: app.data.Url + 'comment/commentLists',
      data: {
        goodsid: options.id,
        type:2
      },
      method: 'get',
      success: function (res) {
        if (res.data.data.length != '') {
          that.setData({
            comment: res.data.data[0],
            comments: res.data.data,
            goodsid: options.id,
            commenttype:true
          })
        }else{
          that.setData({
            commenttype:false
          })
        }

      }
    })
    //优惠券
    wx.request({
      //url: app.data.Url + 'coupon/usercoupon',
      url: 'https://zhapp.4hl.cn/app/Coupon/couponApp',
      data: {
        uid: wx.getStorageSync('userInfo').uid,
        bid: app.data.address.id,
        youtype: 1
      },
      method: 'get',
      success: function (res) {
        that.setData({
          sale: res.data
        })
      }
    })
    //成交记录
    wx.request({
      //url: app.data.Url + 'coupon/usercoupon',
      url: 'https://zhapp.4hl.cn/app/goods/DealRecord',
      data: {
        goodsid: options.id,
        bid: app.data.address.id,
      },
      method: 'post',
      success: function (res) {
        // console.log(res.data.data)
        that.setData({
          buyNum: res.data.data
        })
      }
    })
  },
  //yj
  get_sale: function (e) {
    let that = this;
    if (e.currentTarget.dataset.num != 0){
      wx.showToast({
        icon: 'none',
        title: '已领过优惠券',
      })
    }else{
    wx.request({
      //url: app.data.Url + 'coupon/usercoupon',
      url: 'https://zhapp.4hl.cn/app/Coupon/getcoupon',
      data: {
        uid: wx.getStorageSync('userInfo').uid,
        disid: e.currentTarget.dataset.id,
        youtype: 1
      },
      method: 'get',
      success: function (res) {
        if(res.code==1){
          wx.showToast({
            title: '领取成功',
          })
        }else{
          wx.showToast({
            title: res.msg,
            icon:'none'
          })
        }
        wx.request({
          //url: app.data.Url + 'coupon/usercoupon',
          url: 'https://zhapp.4hl.cn/app/Coupon/couponApp',
          data: {
            uid: wx.getStorageSync('userInfo').uid,
            bid: app.data.address.id,
            youtype: 1
          },
          method: 'get',
          success: function (res) {
            that.setData({
              buyNum: res.data
            })
          }
        })
        // that.setData({
        //   sale: res.data
        // })
      }
    })
  }
    return false;
    let index = e.target.dataset.index;
    this.data.sale[index].isSelect = !this.data.sale[index].isSelect;

    // src = this.data.sale[index].src
    if (this.data.sale[index].isSelect) {
      // this.setData({
      this.data.sale[index].src = "../image/has_get_03.png"
      // })
    }
    this.setData({
      sale: this.data.sale,
    })
  },
  click: function () {
    let that = this;
 
 
    var type = that.data.Type;
    var goodsid = that.data.detail.goodsid;
    var num = that.data.num
    var price = that.data.names.price;
    var names = that.data.names;        //规格
    if (names.stock <=0){
      wx.showToast({
        title: '库存不足',
      })
      return false;
    } else if (num > names.stock){
      wx.showToast({
        title: '库存超出购买选择',
      })
      return false;
    }
    if (type == 0) {
      app.navTo('../confirm/confirm?goodsid=' + goodsid + '&num=' + num + '&guige=' + that.data.names.rule + '&price=' + price + '&attrid=' + that.data._number)
    } else {
      wx.request({
        url: app.data.Url + 'cart/cartadd',
        data: {
          goodsid: goodsid,
          num: num,
          uid: wx.getStorageSync('userInfo').uid,
          rule: that.data.names.rule,
          attrid: that.data.names.attrid,
          sq_bid: app.data.address.id
        },
        method: 'post',
        success: function (res) {
          if (res.data.code == 1) {
            wx.showToast({
              title: '添加成功！',
            })
            that.setData({
              display_confirm:"none"
            })
          }
        }
      })
    }

  },
  share: function () {

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  // onShow: function () {

  // },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (opt) {
    let share_type = opt.target.dataset.type
    let that = this;
    console.log(share_type);
    that.setData({
      show_haibao: false
    })
    return;
    return{
      title:'当前商品',
      path:'lujing',
      imageUrl:"http://img.gek6.com/FoOvlzY6B8m46P-V1cvtFVeyibMe"
    }
  },
})